package com.yinxin.controller;

import com.yinxin.Enum.ResultEnum;
import com.yinxin.Util.ResultUtil;
import com.yinxin.entity.Result.Result;
import com.yinxin.entity.User;
import com.yinxin.entity.params.userParam;
import com.yinxin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: YinXin
 * @Date: 2021/9/10 19:52
 * @Version: 1.0
 * @description:
 */

@RestController
@ResponseBody
@RequestMapping("/usercrud")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/getuser")
    public Result getuser(){
        Result result = userService.getUserInfo();
        return result;
    }

    @PostMapping(value = "/deleteById")
    public Result deleteById(Integer id){
        Result result=userService.deleteUser(id);
        return result;
    }

    @PostMapping(value = "/insertUser")
    public Result insertUser(@RequestBody userParam param){
        if(param.getType()==1){
            User user=param.getUser();
            Result result=userService.insertUser(user);
            return result;
        }else {
            //用枚举对一些重复的不会改变的信息复用便于维护
            Result error = ResultUtil.error(ResultEnum.ERROR_TYPE.getCode(), ResultEnum.ERROR_TYPE.getMsg());
            return error;
        }
    }

    @PostMapping(value = "/updateUser")
    public Result updateUser(@RequestBody userParam param){
        if(param.getType()==2){
            User user=param.getUser();
            Result result=userService.updateUser(user);
            return result;
        }else {
            //用枚举对一些重复的不会改变的信息复用便于维护
            Result error = ResultUtil.error(ResultEnum.ERROR_TYPE.getCode(), ResultEnum.ERROR_TYPE.getMsg());
            return error;
        }
    }
}
